//
//  YWCViewController.m
//  YaDiuDiu
//
//  Created by 欧洪飞 on 2017/9/1.
//  Copyright © 2017年 jy. All rights reserved.
//

#import "YWCViewController.h"
#import "HJCarDeailViewController.h"
#import "YJDCell.h"
#import "CompleteOrderDetailsViewController.h"

@interface YWCViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong)UITableView *tableView;

@property (nonatomic, strong) NSArray *urlArray;

@property (nonatomic, strong) NSMutableArray *orderArray;

@end

@implementation YWCViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.view.backgroundColor = HexColorInt32_t(F2F2F2);
    [self showNav:@"已完成"];
    [self loadUI];
    
    [self loadData];
}

- (void)loadUI{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, ScreenWidth, ScreenHeight-64) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView setBackgroundColor:HexColorInt32_t(F2F2F2)];
    [self.view addSubview:_tableView];
    
    self.urlArray = [[NSArray alloc]initWithObjects:@"",@"",@"/App/Ordermanage/zxbuycarorder",@"/App/Ordermanage/wjorderwashcar",@"/App/Ordermanage/wjhousingorder",@"/App/Ordermanage/wjontheorder",@"/App/Ordermanage/newcoolieorder",@"/App/Ordermanage/wjcleaningorder",@"/App/Ordermanage/wjhousekeepingorder",@"/App/Ordermanage/wjunlockingorder",@"/App/Ordermanage/wjvegetablesorder", nil];
    
}
//已完成
- (void)loadData{
    [SVProgressHUD show];
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
    NSMutableDictionary *parmas = [NSMutableDictionary dictionary];
    [parmas setValue:_businessId forKey:@"merchantid"];
//    parmas[@"p"] = 
    NSLog(@"parmas:%@",parmas);
    NSInteger urlIndex = [self.merType integerValue];
    NSString *urlString = [self.urlArray objectAtIndex:urlIndex];
    NSString *url = [NSString stringWithFormat:@"%@/App/Ordermanage/wcbuycaroder",urlHead];
    NSLog(@"url:%@",url);
    [[AFHTTPSessionManager manager] GET:url parameters:parmas success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"已完成订单res:%@",responseObject);
        NSString *error = [responseObject objectForKey:@"error"];
        NSString *mssage = [responseObject objectForKey:@"mssage"];
        NSMutableArray *result = [responseObject objectForKey:@"result"];
        if ([error integerValue] == 200 && [result isKindOfClass:[NSArray class]]) {
            self.orderArray = [result mutableCopy];
            [self.tableView reloadData];
            
        }
        
        [SVProgressHUD dismiss];
        [LICGToast showWithText:mssage];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error:%@",error);
        [SVProgressHUD dismiss];
    }];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.orderArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellId = @"Cell";
    YJDCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"YJDCell" owner:nil options:nil] lastObject];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    cell.btnjd.hidden = YES;
    cell.labState.text = @"订单已完成";
    cell.labState.textColor = [UIColor darkGrayColor];
    
    NSDictionary *itemDic = [self.orderArray objectAtIndex:indexPath.row];
    [cell.labDDH setText:[NSString stringWithFormat:@"订单号：%@",[itemDic objectForKey:@"ordernumber"]]];
    [cell.labSth setText:[itemDic objectForKey:@"ordername"]];
    [cell.labDate setText:[self getNewDate:[itemDic objectForKey:@"addtime"]]];
    [cell.labPrice setText:[itemDic objectForKey:@"ordermoney"]];
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 130.0;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if ([self.merType isEqualToString:@"3"]) {
        NSDictionary *itemDic = [self.orderArray objectAtIndex:indexPath.row];
        HJCarDeailViewController *vc = [[HJCarDeailViewController alloc]init];
        vc.meri_id = self.businessId;
        vc.merchantid = itemDic[@"id"];
        vc.type = 2;
        [self.navigationController pushViewController:vc animated:YES];
    }else {
        CompleteOrderDetailsViewController *com = [[CompleteOrderDetailsViewController alloc]init];
        NSDictionary *itemDic = [self.orderArray objectAtIndex:indexPath.row];
        com.valueDic = itemDic;
        com.businessId = self.businessId;
        [self.navigationController pushViewController:com animated:YES];
    }
    
    
}

- (NSMutableArray *)orderArray{
    if (!_orderArray) {
        _orderArray = [[NSMutableArray alloc]init];
    }
    return _orderArray;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
